//
//  ICGroupFileController.m
//  ICome
//
//  Created by ENN on 16/8/16.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICGroupFileController.h"
#import "ICEmptyStyleView.h"
#import "ICGroupFileCell.h"
#import "ICFileScanController.h"
#import "ICFileTool.h"
#import "ICHeaderFile.h"

@interface ICGroupFileController ()<UITableViewDataSource,UITableViewDelegate,GroupFileDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong)ICEmptyStyleView *emptyView;

@end

@implementation ICGroupFileController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNav];
    
    [self setupUI];
    
    [self loadData];
    
}

- (void)setupNav
{
    self.title = @"会话文件";
}

- (void)setupUI
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, APP_Frame_Height-64) style:UITableViewStylePlain];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate   = self;
    [self.view addSubview:tableView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)loadData
{
    WEAKSELF
    [ICMessageDatabase fileMessageWithGid:_gId finish:^(NSArray *array, NSError *error) {
        if (array.count == 0) {
            weakSelf.emptyView.hidden = NO;
        } else {
             weakSelf.emptyView.hidden = YES;
            weakSelf.dataArr = (NSMutableArray *)array;
        }
        [weakSelf.tableView reloadData];
    }];
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICGroupFileCell *cell = [ICGroupFileCell cellWithTableView:tableView];
    cell.delegate         = self;
    cell.message          = self.dataArr[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}


#pragma mark - GroupFileDelegate

- (void)isFileExisted:(ICGroupFileCell *)cell isExisted:(BOOL)isExisted fileKey:(NSString *)fileKey fileName:(NSString *)fileName
{
    NSString *filePath = [ICFileTool filePathWithName:fileKey orgName:[fileName stringByDeletingPathExtension] type:[fileName pathExtension]];
    if (isExisted) {
        [self openFile:filePath fileName:fileName];
    } else {
        [self getFile:cell filePath:filePath fileKey:fileKey fileName:fileName];
    }
}

- (void)getFile:(ICGroupFileCell *)cell filePath:(NSString *)filePath fileKey:(NSString *)fileKey fileName:(NSString *)fileName
{
    cell.progressView.hidden = NO;
    WEAKSELF
    [[ICNetworkManager sharedInstance] getFile:fileKey progress:^(NSProgress *pro) {
        dispatch_async(dispatch_get_main_queue(), ^{
            cell.progressView.progress = pro.fractionCompleted;
        });
    } success:^(id object) {
        NSData *fileData = [NSData dataWithData:object];
        BOOL result =[fileData writeToFile:filePath atomically:NO];
        if (!result) NSLog(@"write file failued!");
        cell.progressView.hidden = YES;
        [cell.seeBtn setTitle:@"查看" forState:UIControlStateNormal];
        [weakSelf openFile:filePath fileName:fileName];
    } failure:^(NSError *error) {
        cell.progressView.hidden = YES;
        NSLog(@"download file error");
    }];
}

- (void)openFile:(NSString *)filePath fileName:(NSString *)fileName
{
    ICFileScanController *fileVC = [[ICFileScanController alloc] init];
    fileVC.filePath = filePath;
    fileVC.orgName  = fileName;
    [self.navigationController pushViewController:fileVC animated:YES];
}

#pragma mark - Getter

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(ICEmptyStyleView *)emptyView {
    if (!_emptyView) {
        _emptyView = [[ICEmptyStyleView alloc] initWithTitle:@"暂无本地文件" placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_message_empty"] imageURL:@""];
        _emptyView.hidden = YES;
        _emptyView.frame = CGRectMake(0, 0, App_Frame_Width, APP_Frame_Height);
        [self.view addSubview:_emptyView];
    }
    return _emptyView;
}





@end
